package com.ht.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ht.data.Result;
import com.ht.domin.bo.MembersBO;
import com.ht.domin.po.MembersOrder;
import com.ht.domin.vo.OrderPayment;
import com.ht.dto.UserDTO;
import com.ht.mapper.MembersMapper;
import com.ht.service.MembersService;
import com.ht.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class MembersServiceImpl implements MembersService {

    @Resource
    private MembersMapper membersMapper;

    @Override
    public Result addMembersOrder(MembersBO bo, UserDTO userDTO) {
        try {
            IdWorker worker = new IdWorker();
            //订单id
            long membersId = worker.nextId();
            //创建时间
            Date date = new Date();
            //会员结束时间
            DateTime expireTime = DateUtil.offsetDay(date, 30);
            //订单号
            String orderNum = String.valueOf(worker.nextId());
            MembersOrder membersOrder = new MembersOrder();
            membersOrder.setMembersId(membersId);//订单id
            membersOrder.setPayment(bo.getPayment());//实付金额
            membersOrder.setPaymentType("1");//默认为1  在线支付
            membersOrder.setStatus("1");//1、未支付  2、已支付
            membersOrder.setCreateTime(date);//
            membersOrder.setMembersType(bo.getMembersType());//会员类型
            membersOrder.setUserId(bo.getUserId());//用户id
            membersOrder.setReceiverMobile(bo.getReceiverMobile());//用户手机号
            membersOrder.setSourceType("1");//订单来源 1、app（默认） 2、pc
            membersOrder.setOrderNum(orderNum);
            membersOrder.setExpireTime(expireTime);
            //membersMapper.insert(membersOrder);
            System.out.println(membersOrder);

            /**
             * 支付宝回调需要参数
             */
            OrderPayment orderPayment = new OrderPayment();
            orderPayment.setWIDout_trade_no(orderNum);//商户订单号
            orderPayment.setWIDtotal_amount(String.valueOf(bo.getPayment()));//付款金额
            orderPayment.setWIDsubject("华佗在线");
            orderPayment.setWIDbody("无");
            return Result.OK().setData(orderPayment);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.ERROR();
    }

    /**
     * 支付宝回调增加订单流水号
     * @param out_trade_no
     * @param trade_no
     */
    @Override
    public void updateTradeNumByOrderNum(String out_trade_no, String trade_no) {
        membersMapper.updateTradeNumByOrderNum(out_trade_no,trade_no);
    }

    /**
     * 修改用户支付状态
     * @param out_trade_no
     */
    @Override
    public void updateStatusByOrderNum(String out_trade_no) {
        membersMapper.updateStatusByOrderNum(out_trade_no);
    }

    @Override
    public List<MembersOrder> findExpireTime() {
        List<MembersOrder> membersOrderList = membersMapper.selectList(null);
        return membersOrderList;
    }

    /**
     * 会员过期修改状态
     * @param orderNum
     */
    @Override
    public void updateStatus(String orderNum) {
        membersMapper.updateStatus(orderNum);
    }
}
